<?php
session_start();
require($_SERVER['DOCUMENT_ROOT'].'/resources/lib/database.php');
require($_SERVER['DOCUMENT_ROOT'].'/resources/lib/functions.php');
require($_SERVER['DOCUMENT_ROOT'].'/resources/lib/class.admin.php');

ob_start("sanitize_output"); 
$admin = new admin();
$admin->canView($_SERVER['PHP_SELF']);

$directory = opendir($_SERVER['DOCUMENT_ROOT'].'/themes/');
while (($file = readdir($directory)) !== false) {
	if (is_dir($_SERVER['DOCUMENT_ROOT'].'/themes/'.$file) && ($file != ".") && ($file != "..")) {
		$themes[]=$file;
	}
}
closedir($directory);
?>
<div class="oneem">
<?php if(file_exists($_SERVER['DOCUMENT_ROOT'].'/admin/update.php')) { ?>
<div style="float:right;">
<h2 class="form-heading">Update site</h2>
<form class="inline" action="/admin/update.php">
<input type="hidden" class="settings" value='{"buttonWaitingValue":"Updating","updateNode":"#response"}' />	
<input type="submit" class="button" value="Update the site" />

</form>
</div>
<?php } ?>

<h1>Site settings.</h1>
<form class="inline" action="/admin/resources/php/requests/actions/save-site-settings.php">
<input type="hidden" class="settings" value='{"buttonWaitingValue":"Updating","updateNode":"#response"}' />	
<input type="hidden" name="settings_site_id" id="settings_site_id" value="<?php echo((string)$admin->site_settings['_id']) ?>" />
<table width="100%" cellspacing="2" cellpadding="2">
	<tr valign="top">
    	<td width="9%"><strong>Site Name:</strong></td>
        <td width="91%"><input type="text" name="settings_site_name" id="settings_site_name" class="form-field req" style="width:250px;" value="<?php echo($admin->site_settings['name']); ?>" /></td>
    </tr>
    <tr valign="top">
    	<td width="9%"><strong>Site URL:</strong></td>
        <td width="91%"><input type="text" name="settings_site_url" id="settings_site_url" class="form-field req" style="width:250px;" value="<?php echo($admin->site_settings['url']); ?>" /></td>
    </tr>
    <tr valign="top">
    <td width="9%"><strong>Current Theme:</strong></td>
        <td width="91%"><select name="settings_site_theme" id="settings_site_theme" class="form-field req" style="padding-right:0px;width:156px;">
        	<?php 
				foreach($themes as $theme) {
					$selected=($theme==$admin->site_settings['folder_path']) ? ' selected="selected"' : '';
						echo('<option value="'.$theme.'"'.$selected.'>'.$theme.' (/themes/'.$theme.'/)</option>');	
				}
			?>
        
        </select></td>
    
    </tr>
    
</table>
<input type="submit" class="button" value="Save settings" />
</form>

<h2 style="margin-top:2em;">Upload a new theme</h2>
<form enctype="multipart/form-data" method="post" action="/admin/resources/php/requests/actions/upload_theme.php">
	<input type="file" name="theme" id="theme" /><br /><br /><input type="submit" class="button" value="Upload theme" />
<p class="hint">Your theme must have no spaces in the folder structure</p>
</form>
<h2 style="margin-top:2em;">Logo</h2>

<form enctype="multipart/form-data" method="post" action="/admin/resources/php/requests/actions/upload_logo.php">
	<input type="file" name="logo" id="logo" /><br /><br /><input type="submit" class="button" value="Upload Logo" />
<p class="hint">Your logo must have no spaces in the name (common sense really!)</p>
</form>
<h2>Current Logo:</h2>
<img src="/images/<?php echo($admin->site_settings['logo']['name']); ?>" />
</div>
